/*
Copyright 2024 MySQL Parser Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
	"fmt"
	"log"

	"mysql-parser/sqlparser"
)

func main() {
	fmt.Println("=== 聚合函数查询示例 ===")
	fmt.Println()

	// 基础聚合函数示例
	aggregateQueries := []string{
		"SELECT COUNT(*) FROM users",
		"SELECT COUNT(DISTINCT email) FROM users",
		"SELECT SUM(price) FROM products",
		"SELECT AVG(age) FROM users",
		"SELECT MIN(price), MAX(price) FROM products",
		"SELECT COUNT(*), AVG(price) FROM products WHERE category = 'Electronics'",
	}

	for i, sql := range aggregateQueries {
		fmt.Printf("聚合示例 %d: %s\n", i+1, sql)
		
		// 解析SQL
		stmt, err := sqlparser.Parse(sql)
		if err != nil {
			log.Printf("解析错误: %v", err)
			continue
		}

		// 提取表名
		tables := sqlparser.ExtractAllTables(stmt)
		fmt.Printf("  涉及的表: %v\n", tables)

		// 聚合分析
        aggAnalysis := sqlparser.AnalyzeAggregation(stmt)
        fmt.Printf("  聚合分析: 包含聚合=%v, 聚合函数=%v\n",
            aggAnalysis.HasAggregation, aggAnalysis.Functions)

		// 查询复杂度分析
		complexity := sqlparser.AnalyzeQueryComplexity(stmt)
		fmt.Printf("  复杂度: %s (分数: %d)\n",
			complexity.ComplexityLevel, complexity.ComplexityScore)

		fmt.Println()
	}

	// GROUP BY示例
	fmt.Println("=== GROUP BY查询示例 ===")
	groupByQueries := []string{
		"SELECT category, COUNT(*) FROM products GROUP BY category",
		"SELECT category, AVG(price) FROM products GROUP BY category",
		"SELECT user_id, COUNT(*) as order_count FROM orders GROUP BY user_id",
		"SELECT YEAR(created_at), COUNT(*) FROM orders GROUP BY YEAR(created_at)",
		"SELECT category, MIN(price), MAX(price) FROM products GROUP BY category ORDER BY category",
	}

	for i, sql := range groupByQueries {
		fmt.Printf("GROUP BY示例 %d: %s\n", i+1, sql)
		
		stmt, err := sqlparser.Parse(sql)
		if err != nil {
			log.Printf("解析错误: %v", err)
			continue
		}

		tables := sqlparser.ExtractAllTables(stmt)
		fmt.Printf("  涉及的表: %v\n", tables)

		aggAnalysis := sqlparser.AnalyzeAggregation(stmt)
        fmt.Printf("  聚合分析: 包含聚合=%v, 有GROUP BY=%v, 聚合函数=%v\n",
            aggAnalysis.HasAggregation, aggAnalysis.HasGroupBy, aggAnalysis.Functions)

		complexity := sqlparser.AnalyzeQueryComplexity(stmt)
		fmt.Printf("  复杂度: %s (分数: %d)\n",
			complexity.ComplexityLevel, complexity.ComplexityScore)

		fmt.Println()
	}

	// HAVING示例
	fmt.Println("=== HAVING查询示例 ===")
	havingQueries := []string{
		"SELECT category, COUNT(*) FROM products GROUP BY category HAVING COUNT(*) > 5",
		"SELECT user_id, SUM(amount) FROM orders GROUP BY user_id HAVING SUM(amount) > 1000",
		"SELECT category, AVG(price) FROM products GROUP BY category HAVING AVG(price) > 100",
		"SELECT YEAR(created_at), COUNT(*) FROM orders GROUP BY YEAR(created_at) HAVING COUNT(*) > 10",
	}

	for i, sql := range havingQueries {
		fmt.Printf("HAVING示例 %d: %s\n", i+1, sql)
		
		stmt, err := sqlparser.Parse(sql)
		if err != nil {
			log.Printf("解析错误: %v", err)
			continue
		}

		tables := sqlparser.ExtractAllTables(stmt)
		fmt.Printf("  涉及的表: %v\n", tables)

		aggAnalysis := sqlparser.AnalyzeAggregation(stmt)
        fmt.Printf("  聚合分析: 包含聚合=%v, 有GROUP BY=%v, 有HAVING=%v\n",
            aggAnalysis.HasAggregation, aggAnalysis.HasGroupBy, aggAnalysis.HasHaving)

		complexity := sqlparser.AnalyzeQueryComplexity(stmt)
		fmt.Printf("  复杂度: %s (分数: %d)\n",
			complexity.ComplexityLevel, complexity.ComplexityScore)

		fmt.Println()
	}

	// 窗口函数示例（如果支持）
	fmt.Println("=== 窗口函数查询示例 ===")
	windowQueries := []string{
		"SELECT name, price, ROW_NUMBER() OVER (ORDER BY price DESC) as rank FROM products",
		"SELECT category, name, price, RANK() OVER (PARTITION BY category ORDER BY price DESC) as category_rank FROM products",
		"SELECT user_id, amount, SUM(amount) OVER (PARTITION BY user_id ORDER BY created_at) as running_total FROM orders",
	}

	for i, sql := range windowQueries {
		fmt.Printf("窗口函数示例 %d: %s\n", i+1, sql)
		
		stmt, err := sqlparser.Parse(sql)
		if err != nil {
			log.Printf("解析错误: %v (窗口函数可能暂不支持)", err)
			continue
		}

		tables := sqlparser.ExtractAllTables(stmt)
		fmt.Printf("  涉及的表: %v\n", tables)

		complexity := sqlparser.AnalyzeQueryComplexity(stmt)
		fmt.Printf("  复杂度: %s (分数: %d)\n",
			complexity.ComplexityLevel, complexity.ComplexityScore)

		fmt.Println()
	}
}